package com.querycenter.serviceimpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.querycenter.mapper.DataObjectManageMapper;
import com.querycenter.model.DataObject;
import com.querycenter.model.DataSource;
import com.querycenter.model.FieldDictionary;
import com.querycenter.serviceinter.DataObjectManageServiceInter;

public class DataObjectManageServiceImpl
		implements DataObjectManageServiceInter {
	private DataObjectManageMapper domm = null;

	public DataObjectManageMapper getDomm() {
		return domm;
	}

	public void setDomm(DataObjectManageMapper domm) {
		this.domm = domm;
	}

	@Override
	public void addDataObject(DataObject dobject) {
		domm.addDataObject(dobject);
	}

	@Override
	public List<FieldDictionary> getField(DataObject dobject)
			throws ClassNotFoundException, SQLException {
		String sql = null;
		if (dobject.getType() == 1) {
			sql = "select * from " + dobject.getDescription();
		} else {
			sql = dobject.getDescription();
		}

		// 以下连接池,根绝数据源类型选择连接池
		String jdbcUrl = "jdbc:oscar://localhost:2003/osrdb";
		String userName = "app";
		String password = "123456";
		Class.forName("com.oscar.Driver");
		Connection con = DriverManager.getConnection(jdbcUrl, userName,
				password);
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		java.sql.ResultSetMetaData rsmd = (java.sql.ResultSetMetaData) rs
				.getMetaData();
		// 以上连接池

		List<FieldDictionary> fdList = new ArrayList<FieldDictionary>();
		for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
			FieldDictionary fd = new FieldDictionary();
			fd.setDataobjectid(dobject.getId());
			fd.setField(rsmd.getColumnName(i));
			fd.setIndex(i);
			fd.setFieldtype(rsmd.getColumnType(i));// 类型分类，归为1、数字2、字符3、日期
			fd.setFieldsize(rsmd.getColumnDisplaySize(i));
			fd.setAuthorityflag(2);
			fdList.add(fd);
		}
		stmt.close();
		con.close();
		return fdList;
	}

	@Override
	public void addField(FieldDictionary fd) {
		domm.addField(fd);
	}

	@Override
	public void addDataSource(DataSource ds) {
		domm.addDataSource(ds);
	}

	@Override
	public int getDataObjectId(DataObject dobject) {
		return domm.getDataObjectId(dobject);
	}

	@Override
	public void updateFieldFlag(FieldDictionary fd) {
		domm.updateFieldFlag(fd);
	}


}
